﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using StillVoiceCommon;
using TeamTime.Classes;

public partial class MasterDefault : System.Web.UI.MasterPage
{
    public string LoggedInMessage { get { return lblUserName.Text; } set { lblUserName.Text = value;} }
    public Security sec;
    public string pagePermissionsMsg;

    protected void Page_Init(object sender, EventArgs e)
    {
        //if (Session["UserID"] == null && Request.LogonUserIdentity.Name.ToUpper().Contains("GARETHFLAPTOP"))
        //{
        //    Session["UserID"] = 1;
        //    Session["UserName"] = "Gareth";
        //}

      sec = new Security(sqlDB.getDBConnString());

        if (Session["UserID"] == null)
        {
            Response.Redirect("login.aspx?URL=" + Request.RawUrl, true);
        }
        else
        {
            sec.UserID = Convert.ToInt32(Session["UserID"]);

            this.LoggedInMessage = "You are logged in as " + Session["UserName"] + " - " + Session["CompanyName"];
            //if (Session["EmployeeName"] != Session["UserName"])
            //    this.lblUserName.Text += " on behalf of " + Session["EmployeeName"];
            if (!Page.IsPostBack)
            {
                string s = Parent.Page.Request.RawUrl.Split('?')[0].ToLower();
                s = s.Replace(@"/", "").Replace("tepims", "").Replace("tepweb", "").Replace("teptest", "").Replace("teptrain", "");
                if (!sec.CanDo(s) && !s.Contains("errorpage"))
                    Response.Redirect("ErrorPage.aspx?msg=You do not have permissions to the following function:" + 
                                        this.Parent.Page.Title + "(" + s + ")");

                this.LeftMenu1.buildMenu();
            }
        }

    }
  
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }

    protected void cmdLogout_Click(System.Object sender, System.EventArgs e)
    {
        Session["UserID"] = null;
        Session.Remove("MenuDS");
        ////release this user's menu from Cache
        //if (Cache["Menu" + Session["UserID"]] != null)
        //        Cache["Menu" + Session["UserID"]] = null; 
        //FormsAuthentication.SignOut()
        Response.Redirect("login.aspx"); //, true)
    }


  
}
